const express = require('express');
const fs = require('fs')
const router = express.Router();

const { Goods } = require('../db/Schemas/goods')
const { Category } = require('../db/Schemas/Category')
const { CategorySub } = require('../db/Schemas/CategorySub')

// 导入数据
router.get('/insertAllGoodsInfo', async(req, res, next) => {
    fs.readFile('./newGood.json', 'utf8', (err, data) => {
        if (err) {
            console.log('读取文件失败了')
        } else {
            // console.log(data)
            data = JSON.parse(data)
            let count = 0
            data.map((item) => {
                let newGoods = new Goods(item)
                newGoods.save().then(() => {
                    count++
                    console.log('保存成功' + count)
                }).catch((err) => {
                    console.log(MediaStreamErrorEvent)
                })
            })
        }

    })
    res.json({
        code: 200,
        title: 'success'
    })
});
router.get('/insertAllCategory', async(req, res, next) => {
    fs.readFile('./data_json/Category.json', 'utf8', (err, data) => {
        if (err) {
            console.log('读取文件失败了')
        } else {
            console.log(data)
            data = JSON.parse(data)
            let count = 0
            data.RECORDS.map((item) => {
                let newCategory = new Category(item)
                newCategory.save().then(() => {
                    count++
                    console.log('保存成功' + count)
                }).catch((err) => {
                    console.log(MediaStreamErrorEvent)
                })
            })
        }

    })
    res.json({
        code: 200,
        title: 'success'
    })
});
router.get('/insertAllCategorySub', async(req, res, next) => {
    fs.readFile('./data_json/CategorySub.json', 'utf8', (err, data) => {
        if (err) {
            console.log('读取文件失败了')
        } else {
            // console.log(data)
            data = JSON.parse(data)
            let count = 0
            data.RECORDS.map((item) => {
                let newCategorySub = new CategorySub(item)
                newCategorySub.save().then(() => {
                    count++
                    console.log('保存成功' + count)
                }).catch((err) => {
                    console.log(MediaStreamErrorEvent)
                })
            })
        }

    })
    res.json({
        code: 200,
        title: 'success'
    })
});
router.get('/home', async(req, res, next) => {
    fs.readFile('./data_json/home.json', 'utf8', (err, data) => {
        if (err) {
            console.log('读取文件失败')
        } else {
            data = JSON.parse(data)
            res.json({
                code: 200,
                data
            })
        }
    })
})

router.get('/getCateGoryList', async(req, res) => {
    const CateGoryList = await Category.find()
    res.send({
            code: 200,
            message: CateGoryList
        })
        // res.send(CateGoryList)
})
router.post('/getCategorySubList', async(req, res) => {
        console.log(req.body)
        let categoryId = req.body.categoryId
        const CategorySubList = await CategorySub.find({ MALL_CATEGORY_ID: categoryId })
        res.send({
            code: 200,
            message: CategorySubList
        })
    })
    /**根据类别获取商品列表 */

router.post('/getGoodsListByCategorySubID', async(req, res) => {
        try {
            let categorySubId = req.body.categorySubId //子类别ID
            console.log(categorySubId)
            let page = req.body.page //当前页数
            console.log(page)
            let num = 10 //每页显示数量
            let start = (page - 1) * num //开始位置
            let result = await Goods.find({ SUB_ID: categorySubId })
                .skip(start).limit(num).exec()
            console.log(result)
            res.send({ code: 200, message: result })
        } catch (error) {
            res.send({ code: 500, message: error })
        }

    })
    //**获取商品详情信息的接口
router.post('/getDetailGoodsInfo', async(req, res) => {
    try {
        let goodsId = req.body.goodsId
        console.log(goodsId)
        let result = await Goods.findOne({ ID: goodsId }).exec()
        res.send({ code: 200, message: result })
    } catch (error) {
        res.send({ code: 500, message: error })
    }
})
module.exports = router;